package com.hnl.action.activity;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hnl.core.HNLPageDispatch;
import com.hnl.core.aware.HNLAuthAware;
import com.hnl.core.aware.HNLTalentAware;
import com.hnl.core.aware.MiscTypeAware;
import com.hnl.dao.ActivityDAO;
import com.hnl.dao.DAOException;
import com.hnl.dao.HNLDAOFactory;
import com.hnl.to.Activity;
import com.hnl.to.User;

@SuppressWarnings("serial")
public class UpdateActivity extends HttpServlet implements HNLAuthAware,
		HNLTalentAware, MiscTypeAware, HNLPageDispatch {
	public static final Logger LOG = LoggerFactory
			.getLogger(UpdateActivity.class);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			String activityID = req.getParameter(ATTRIBUTE_ACTIVITY_ID);
			String talentID = req.getParameter(ATTRIBUTE_TALENT_ID);

			HttpSession session = ((HttpServletRequest) req).getSession();
			User user = (User) session.getAttribute(ATTRIBUTE_USER);

			ActivityDAO activityDAO = HNLDAOFactory.getDAOFactory(
					HNLDAOFactory.HNLDAOFACTORY).getActivityDAO();

			Date now = new Date();
			Activity act = new Activity();
			act.setActivityID(Integer.valueOf(activityID));
			act.setTalentID(Integer.valueOf(talentID));
			act.setActivityType(Integer.parseInt(req
					.getParameter(PARAM_ACTIVITY_TYPE_DROPDOWN)));
			act.setDescription(req
					.getParameter(PARAM_ACTIVITY_DESCRIPTION_DROPDOWN));
			act.setIsdelete(false);
			act.setCreateDate(now);
			act.setCreateUser(user.getUserID());
			act.setModifyDate(now);
			act.setModifyUser(user.getUserID());
			activityDAO.updateActivity(act);

			req.setAttribute(ATTRIBUTE_TALENT_ID, talentID);
			req.setAttribute(ATTRIBUTE_TAB_ID, ATTRIBUTE_ACTIVITY_INFO_TAB_ID);

			RequestDispatcher dispatcher = req
					.getRequestDispatcher(TALENT_INFO_PAGE);
			dispatcher.forward(req, resp);
		} catch (DAOException e) {
			LOG.error("Error to add activity!!", e);
		}

	}

}
